package mvc_generator.generator.car.struts;

import mvc_generator.generator.MVCGeneratorGeneratorUtils;
import mvc_generator.generator.car.struts.StrutsGenerator;
import mvc_generator.mVCGenerator.AssignedField;
import mvc_generator.mVCGenerator.Form;
import mvc_generator.mVCGenerator.FormValidation;
import mvc_generator.mVCGenerator.ModelMVC;
import mvc_generator.mVCGenerator.SimpleType;
import org.eclipse.emf.common.util.EList;
import org.eclipse.xtend2.lib.StringConcatenation;
import org.eclipse.xtext.generator.IFileSystemAccess;
import org.eclipse.xtext.xbase.lib.StringExtensions;

@SuppressWarnings("all")
public class ServletActionGenerator {
  public void generate(final FormValidation fv, final IFileSystemAccess fsa) {
    ModelMVC modelMVCContainer = MVCGeneratorGeneratorUtils.findModelMVCContainer(fv);
    StringConcatenation _builder = new StringConcatenation();
    CharSequence servletActionContent = _builder;
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("\t");
    _builder_1.append("package servletAction;");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("import javax.servlet.http.HttpServletRequest;");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("import javax.servlet.http.HttpServletResponse;");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("import org.apache.struts.Globals;");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("import org.apache.struts.action.Action;");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("import org.apache.struts.action.ActionForm;");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("import org.apache.struts.action.ActionForward;");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("import org.apache.struts.action.ActionMapping;");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("import org.apache.struts.action.ActionMessage;");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("import org.apache.struts.action.ActionMessages;");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append(StrutsGenerator.domainJavaImport, "	");
    _builder_1.newLineIfNotEmpty();
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("public class ");
    String _validatedFormName = fv.getValidatedFormName();
    String _firstUpper = StringExtensions.toFirstUpper(_validatedFormName);
    _builder_1.append(_firstUpper, "	");
    _builder_1.append("Action extends Action {");
    _builder_1.newLineIfNotEmpty();
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("public ActionForward execute(final ActionMapping pMapping, ActionForm pForm, ");
    _builder_1.newLine();
    _builder_1.append("\t\t\t\t\t\t\t\t ");
    _builder_1.append("final HttpServletRequest pRequest, final HttpServletResponse pResponse) {");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("final ");
    String _validatedFormName_1 = fv.getValidatedFormName();
    String _firstUpper_1 = StringExtensions.toFirstUpper(_validatedFormName_1);
    _builder_1.append(_firstUpper_1, "	");
    _builder_1.append("ValidationForm lForm = (");
    String _validatedFormName_2 = fv.getValidatedFormName();
    String _firstUpper_2 = StringExtensions.toFirstUpper(_validatedFormName_2);
    _builder_1.append(_firstUpper_2, "	");
    _builder_1.append("ValidationForm) pForm;");
    _builder_1.newLineIfNotEmpty();
    _builder_1.append("\t");
    _builder_1.newLine();
    {
      Form _validateForm = MVCGeneratorGeneratorUtils.getValidateForm(fv);
      EList<AssignedField> _assignedFields = _validateForm.getAssignedFields();
      for(final AssignedField assignedField : _assignedFields) {
        _builder_1.append("\t\t");
        _builder_1.append("final ");
        SimpleType _simpleTypeOfAssignement = MVCGeneratorGeneratorUtils.getSimpleTypeOfAssignement(assignedField);
        String _stringRepresentation = _simpleTypeOfAssignement.getStringRepresentation();
        _builder_1.append(_stringRepresentation, "		");
        _builder_1.append(" ");
        String _fieldName = assignedField.getFieldName();
        _builder_1.append(_fieldName, "		");
        _builder_1.append(" = lForm.get");
        String _fieldName_1 = assignedField.getFieldName();
        String _firstUpper_3 = StringExtensions.toFirstUpper(_fieldName_1);
        _builder_1.append(_firstUpper_3, "		");
        _builder_1.append("();");
        _builder_1.newLineIfNotEmpty();
      }
    }
    _builder_1.append("\t\t");
    _builder_1.newLine();
    _builder_1.append("\t\t");
    _builder_1.newLine();
    _builder_1.append("\t\t");
    _builder_1.append("/*");
    _builder_1.newLine();
    _builder_1.append("\t\t ");
    _builder_1.append("*  USAGE : return pMapping.findForward(\"<yourViewName>\"); ");
    _builder_1.newLine();
    _builder_1.append("\t\t ");
    _builder_1.append("*/");
    _builder_1.newLine();
    _builder_1.append("\t\t");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.append("}\t\t");
    _builder_1.newLine();
    servletActionContent = _builder_1;
    String _name = modelMVCContainer.getName();
    String _plus = (_name + "Struts");
    String _plus_1 = (_plus + StrutsGenerator.servletActionPath);
    String _validatedFormName_3 = fv.getValidatedFormName();
    String _plus_2 = (_plus_1 + _validatedFormName_3);
    String _plus_3 = (_plus_2 + "Action.java");
    fsa.generateFile(_plus_3, servletActionContent);
  }
}
